// pages/settlement/ticket_detail/ticket_detail.js
const HOST = require('../../../config.js').host
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: '', // openid
    ticketList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this;
    wx.login({
      success(res) {
        if (res.code) {
          wx.request({
            url: HOST + 'v1/login/' + res.code,
            method: 'GET',
            success(res) {
              if (res.data.sysCode === 1) {
                self.setData
                  ({
                    userId: res.data.data,
                  })
                self.getgiveCoupon(res.data.data);
                // self.scanCoupons(res.data.data);
              }
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })   
  },
  // 获取赠送优惠券
  getgiveCoupon(userId) {
    let that = this
    wx.request({
      url: `${HOST}v1/usercoupons/saveGiftCoupons`,
      method: 'POST',
      async:false,
      data: {
        userId: userId
      },
      success: res => {
        console.log(res);
        that.scanCoupons(userId);
      }
    })
  },
  scanCoupons(userId) {
    let self = this;
    wx.request({
      url: HOST + 'v1/coupons/findByUserId/' + userId,
      method: 'GET',
      success(res) {
        let ticketList = [];
        if (res.data.sysCode === 1) {
          res.data.data.forEach(item => {
            if (item.couponType == 1) ticketList.push(item)
          })
          self.setData({
            ticketList: ticketList
          })
        }
      }
    })
  }, 
  navigateTo(e) {
    wx.navigateTo({
      url: `../../detail/detail?id=${e.currentTarget.dataset.productid}`
    })
  },

  goMycenter:()=>{
    wx.switchTab({
      url: '../../user/user',
    })
  },
  getMore() {
    wx.switchTab({
      url: '../../home/home',
    })
  }
  ,
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})